#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
int main()
{
    //1.创建管道
    int Fildes[2];
    int n  = pipe(Fildes);
    //2.创建子进程
    pid_t id = fork();
   if(id < 0) exit(-1);
   //3.父写子读
   else if(id == 0)
   {
       //child
        close(Fildes[1]);
        //todo
       char buffer[1024];
       while(1)
       {
           sleep(1);
            
            int n = read(Fildes[0],buffer,sizeof(buffer) - 1);
            if(n < 0)
            {
                printf("%s","read fail\n");
            }
            buffer[n] = 0;
            printf("%s\n",buffer);
        }
        exit(0);
    }
   //father
   close(Fildes[0]);
    const char* str = "I am father";
    while(1)
    {
       int n =  write(Fildes[1],str,strlen(str));
       if(n != (int)strlen(str))
       {
           printf("%s","write fail\n");
       }
       sleep(1);
    }
    return 0;
}
